<?php
/**
 * 	app后台首页
 *  =============================
 * 	后台父类
 * 	=============================
 *	Author: 褚天恩
 *  Date: 2017-10-09
 * @author Administrator
 *
 */
namespace app\walletyhbbadmin\controller;
use Think\Db;
class Manage extends AdminBase {
    public function _initialize(){
        parent::_initialize();
    }

    //判断权限
    private function auth(){
        $admin=$this->admin;
        if($admin['admin_id']!=1){
            $this->error('只有超级管理员可以进入此页面');exit();
        }
    }
    /**
     * 管理员列表
     */
    public function index(){
        $this->auth();
        $admin = DB::name('Admin');
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $admin->paginate(5);
    	$page = $list->render ();
        // 赋值数据集
        $this->assign('list',$list);
        // 赋值分页输出
        $this->assign('page',$page);
        return $this->fetch('manage/index');
    }
    /**
     * 添加管理员
     */
    public function addAdmin ()
    {
        return $this->fetch('manage/addAdmin');
    }
    public function doaddAdmin(){
            $user_name = input('user_name');
            $password=md5(input('password'));
            $email =input('email');
            $pwd = input('password');
            if(empty($user_name)||empty($pwd)||empty($email)){
                $this->error('请补全信息');
            }
            $data['user_name']=$user_name;
            $data['password']=$password;
            $data['email']=$email;
            $data['add_time'] = time();
            $rs=DB::name('Admin')->where("user_name='$user_name'")->find();
            if($rs){
                $this->error('用户名称已存在');
            }
            $res=DB::name('Admin')->insert($data);
            $res1=DB::name('Admin_us')->insert($data);
            $res2=DB::name('Admin_jp')->insert($data);
            if($res && $res1 && $res2){
                $a = '添加管理员';
                $this->daily($a);
                $this->success('添加成功','manage/index');
            }else{
                $this->error('添加失败','manage/addAdmin');
            }
    }
    //修改权限页面
    public function showStatus(){
        $this->auth();
        $admin_id = input('id');
        $data['status']= input('status');
        $res = DB::name('Admin')->where('admin_id',$admin_id)->update($data);
        $res1 = DB::name('Admin_us')->where('admin_id',$admin_id)->update($data);
        $res2 = DB::name('Admin_jp')->where('admin_id',$admin_id)->update($data);
        if($res && $res1 && $res2)
        {
            $a='修改权限页面';
            $this->daily($a);
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 修改权限程序
     */
    public function showNav()
    {
        $this->auth();
        $where['admin_id']=$_GET['admin_id'];
        $admin=DB::name('Admin')->where($where)->find();
        $list=explode(',',$admin['nav_list']);
        $nav=DB::name('app_nav')->select();
        foreach ($nav as $k=>$v){
            if(in_array($v['nav_id'],$list)){
                $nav[$k]['status']=1;
            }
        }
        $this->assign('nav',$nav);
        $this->assign('id',$_GET['admin_id']);
        return $this->fetch();
    }
    public function saveNav(){
        $this->auth();
        $where['admin_id']=$_GET['admin_id'];
        $res = DB::name('Admin')->where($where)->find();
        if($_POST['admin_id']){
            if(empty($_POST['nav'])) {
                $data['nav_list'] = '';
            }else{
                $data['nav_list']=implode(',',$_POST['nav']);
            }
            $rs=DB::name('Admin')->where(array('admin_id'=>$_POST['admin_id']))->update($data);
            $rs1=DB::name('Admin_us')->where(array('admin_id'=>$_POST['admin_id']))->update($data);
            $rs2=DB::name('Admin_jp')->where(array('admin_id'=>$_POST['admin_id']))->update($data);
            $a = '修改权限程序';
            $this->daily($a);
            $this->success('修改成功');

        }else{
            $this->error('操作有误');
        }

    }
    /**
     * 修改本账号密码
     */
    public function pwdUpdate(){
        header("Content-type:text/html;charset=utf-8");
        $admin_id = session('admin_id');
        $list = Db::name('Admin')->where('admin_id='.$admin_id)->find();
        if(empty($admin_id)){
            $this->error('操作有误');
            return;
        }
        if(IS_POST){
            if(empty($_POST['old_pwd'])){
                $this->error('请输入原始密码');
                return;
            }
            if(empty($_POST['password'])){
                $this->error('请输入新密码');
                return;
            }
            $old_pwd = md5(input("old_pwd"));
            if($list['password'] != $old_pwd){
                $this->error('您输入的原始密码错误');
                return;
            }
            $data['password']=md5(input("password"));
            $data['pwd_show']=input("password");
            $r = DB::name('Admin')->where('admin_id='.$admin_id)->update($data);
            $r1 = DB::name('Admin_us')->where('admin_id='.$admin_id)->update($data);
            $r2 = DB::name('Admin_jp')->where('admin_id='.$admin_id)->update($data);
            if($r===true){
                $this->error('修改失败');
                return;
            }else{
                $a = '修改本账号密码';
                $this->daily($a);
                $this->redirect('Login/loginout','',1,"<script>alert('修改成功请重新登录')</script>");
                return;
            }
        }else{
            $this->assign('list',$list);
           return $this->fetch();
        }
    }
    /**
     * 删除管理员
     */
    public function delMember(){
        $this->auth();
        if(empty($_GET['admin_id'])){
            $this->error('要删除的ID不存在');
        }
        if($_GET['admin_id']==1){
            $this->error('此ID不可删除');
        }
        $admin = DB::name('Admin');
        $r = $admin->delete($_GET['admin_id']);
        if($r){
            $this->success('删除成功');
        }else{
            $this->error('系统繁忙,请稍后再试');
            return;
        }
    }

    /**
     * 修改信息
     */
    public function  editAdmin()
    {
        $admin_id = input('admin_id');
        $list = DB::name('admin')->where('admin_id',$admin_id)->find();
        $this->assign('list',$list);
        return $this->fetch('manage/editAdmin');
    }
    public function doeditAdmin()
    {
        $admin_id = input('admin_id');
        $list = DB::name('Admin')->where('admin_id',$admin_id)->find();
        $data['user_name'] = input('user_name');
        $data['password'] = md5(input('password'));
        $arr['password'] = input('password');
        if(empty($data['user_name']))
        {
            $this->error('请输入管理员名称');
        }
        if(empty($arr['password']))
        {
            $data['password'] == $list['password'];
        }
        $res = DB::name('Admin')->where('admin_id',$admin_id)->update($data);
        $res = DB::name('Admin_us')->where('admin_id',$admin_id)->update($data);
        $res = DB::name('Admin_jp')->where('admin_id',$admin_id)->update($data);
        if($res)
        {
            $a = '修改信息';
            $this->daily($a);
            $this->success('修改成功','manage/index');
        }else{
            $this->error('修改失败','manage/editAdmin');
        }
    }
}